package ge.telasi.tasks.ui.task;

import ge.telasi.tasks.model.TaskBody;
import ge.telasi.tasks.ui.user.UserLabel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

/**
 * @author dimitri
 */
public class TaskBodyPanel extends JPanel {
    private TaskBody body;
    private JTextArea text;
    private JPanel header;

    public TaskBodyPanel(TaskBody body) {
        this.body = body;
        initComponents();
    }

    private void initComponents() {
        setLayout(new BorderLayout());
        createHeader();
        createText();
        setComponentBorder();
    }

    private void createHeader() {
        header = new JPanel(new BorderLayout(5, 5));
        header.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
        header.setOpaque(true);
        header.setBackground(new Color(200, 200, 200));
        createStatusLabel();
        createOwnerPanel();
        createDateLabel();
        add(header, BorderLayout.NORTH);
    }

    private void createStatusLabel() {
        JLabel lblStatus = new JLabel();
        if (body.getData() == null) {
            lblStatus.setIcon(TaskDecontationUtils.getStatusIcon(body.getTaskStatus()));
        } else {
            lblStatus.setIcon(TaskDecontationUtils.getBodyDataIcon(body.getData()));
        }
        header.add(lblStatus, BorderLayout.WEST);
    }

    private void createDateLabel() {
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        JLabel lblDate = new JLabel(df.format(body.getCreated().getTime()));
        header.add(lblDate, BorderLayout.EAST);
    }

    private void createOwnerPanel() {
        final JLabel lblOwner = new UserLabel(body.getOwner());
        JPanel pnl = new JPanel(new BorderLayout());
        pnl.add(lblOwner, BorderLayout.WEST);
        pnl.setOpaque(false);
        header.add(pnl, BorderLayout.CENTER);
    }

    private void createText() {
        text = new JTextArea();
        text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.setText(body.getBody());
        text.setEditable(false);
        text.setBorder(null);
        add(text, BorderLayout.CENTER);
        text.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    }

    private void setComponentBorder() {
        Border compBorder1 = BorderFactory.createLineBorder(Color.BLACK);
        Border compBorder2 = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        Border compBorder = BorderFactory.createCompoundBorder(compBorder2, compBorder1);
        setBorder(compBorder);
    }

}
